/**
 * Copyright (c) 2021 ZhangXiaoqiu
 * Image3DJs is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
import sensor from '@system.sensor';

export default {
    props: {
        scale: { // 图片放大比例
            default: 1.2
        },
        sensitivity: { // 灵敏度
            default: 1
        },
        imgBackground: { // 下层图片
            default: ""
        },
        imgMid: { // 中层图片
            default: ""
        },
        imgForeground: { // 上层图片
            default: ""
        }
    },
    data: {
        translateX: 0,
        translateY: 0,
        comWidth: 0,
        comHeight: 0,
        maxTranslateX: 0,
        maxTranslateY: 0,
        firstRetGamma: 0,
        firstRetBeta: 0
    },
    computed: {
        translateXBackground() {
            return this.translateX
        },
        translateXForeground() {
            return -this.translateX
        },
        translateYBackground() {
            return this.translateY
        },
        translateYForeground() {
            return -this.translateY
        },
    },
    onActive() {
        var that = this
        // 观察设备方向传感器数据变化
        sensor.subscribeDeviceOrientation({
            interval: 'game',
            success: function (ret) {
                // 记录第一次获取的方向数据
                if (that.firstRetBeta == 0) {
                    that.firstRetGamma = ret.gamma
                    that.firstRetBeta = ret.beta
                }
                // 位移距离 = (当前的方向数据 - 第一次记录的数据) * 灵敏度
                that.setTranslateX((ret.gamma - that.firstRetGamma) * that.sensitivity * 0.7)
                that.setTranslateY((ret.beta - that.firstRetBeta) * that.sensitivity * 0.7)
            },
            fail: function (data, code) {

            }
        });
    },
    onPageShow() {
        // 获取组件尺寸
        this.comWidth = parseInt(this.$refs.stack_image_3d.getBoundingClientRect().width * this.scale)
        this.comHeight = parseInt(this.$refs.stack_image_3d.getBoundingClientRect().height * this.scale)
        // 计算最大可位移距离
        this.maxTranslateX = this.comWidth * (this.scale - 1) / 2 / 2
        this.maxTranslateX = this.comWidth * (this.scale - 1) / 2 / 2
    },
    onDetached() {
        // 取消订阅设备方向传感器数据
        sensor.unsubscribeDeviceOrientation()
    },
    setTranslateX(translateX) {
        // 如果位移距离超过最大可位移距离，则只位移最大可位移距离
        if (translateX < -this.maxTranslateX) {
            this.translateX = -this.maxTranslateX
        } else if (translateX > this.maxTranslateX) {
            this.translateX = this.maxTranslateX
        } else {
            this.translateX = translateX
        }
    },
    setTranslateY(translateY) {
        // 如果位移距离超过最大可位移距离，则只位移最大可位移距离
        if (translateY < -this.maxTranslateY) {
            this.translateY = -this.maxTranslateY
        } else if (translateY > this.maxTranslateY) {
            this.translateY = this.maxTranslateY
        } else {
            this.translateY = translateY
        }
    }
}



